package ortus.media.metadata;

import ortus.*;
import sagex.api.MediaFileAPI;
import sagex.api.ShowAPI;
import sagex.api.AiringAPI;
import sagex.api.Utility;
import sagex.api.Global;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

public class WIZMetadata extends ortus.ortusvars implements IMetadataProvider
{

//MediaType
	public int GetMediaType(Object MediaObject) {
		String Type = MediaFileAPI.GetMediaFileMetadata(MediaObject, "MediaType");
		//ortus.api.DebugLog(TRACE, "MediaType: '" + MediaFileAPI.GetMediaFileMetadata(MediaObject, "MediaType")+ "'");
		if (Type.equalsIgnoreCase("TV")){
			return 1;
		}else if (Type.equalsIgnoreCase("Movie")){
			return 2;
		}else{
			return 0;
		}
	}

	public int GetMediaGroup(Object MediaObject){
		if (IsRecorded(MediaObject)){
			return 0;
		} else if (IsImported(MediaObject)){
			return 1;
		} else {
			return -1;
		}

	}

	public boolean IsTV(Object MediaObject) {
		if (MediaFileAPI.IsTVFile(MediaObject)) {
			return true;
		} else {
			return GetMediaType(MediaObject) == 1;
		}
	}

	public boolean IsTVMovie(Object MediaObject) {
		if (MediaFileAPI.IsTVFile(MediaObject)){
			return MediaFileAPI.IsTVFile(MediaObject) && ShowAPI.GetShowCategory(MediaObject).contains("Movie");
		}else{
			return false;
		}
	}

	public boolean IsImportedTV(Object MediaObject) {
//		ortus.api.DebugLog(TRACE, "IsImportedTV: File: '" + ortus.api.GetFilePathForSegment(MediaObject, 0) + "'");
//		boolean b = !IsRecorded(MediaObject) && IsTV(MediaObject);
//		ortus.api.DebugLog(TRACE, "IsImportedTV: " + b);
//		return b;

		return IsImported(MediaObject) && IsTV(MediaObject);

	}

	public boolean IsRecorded(Object MediaObject) {
//		boolean b = MediaFileAPI.IsTVFile(MediaObject);
//		ortus.api.DebugLog(TRACE, "IsRecorded: " + b);
//		return b;
		
		return MediaFileAPI.IsTVFile(MediaObject);
	}
	
	public boolean IsImported(Object MediaObject) {
//		boolean b = ( MediaFileAPI.IsVideoFile(MediaObject) || MediaFileAPI.IsDVD(MediaObject) ) && !MediaFileAPI.IsTVFile(MediaObject);
//		ortus.api.DebugLog(TRACE, "IsImported: " + b);
//		return b;

		return ( MediaFileAPI.IsVideoFile(MediaObject) || MediaFileAPI.IsDVD(MediaObject) ) && !MediaFileAPI.IsTVFile(MediaObject);
	}

	public boolean IsSeries(Object MediaObject) {
		return false;
	}

	public boolean IsIntelligentRecorded(Object MediaObject) {
		if (!MediaFileAPI.IsTVFile(MediaObject)){
			return false;
		} else if (MediaFileAPI.GetMediaFileAiring(MediaObject) != null && !MediaFileAPI.IsFileCurrentlyRecording(MediaObject)){
			return AiringAPI.IsNotManualOrFavorite(MediaObject);
		} else {
			return Utility.FindElementIndex(Global.GetSuggestedIntelligentRecordings(), MediaObject) != -1;
		}
	}

	public boolean IsMovie(Object MediaObject) {
		if (IsTV(MediaObject)){
			return IsTVMovie(MediaObject);
		} else if (GetMediaType(MediaObject) == 2){
			return true;
		} else if (MediaFileAPI.IsDVD(MediaObject)){
			return true;
		} else {
			return false;
		}
	}




	/* returns true if and only if Mediafile Is DVD object
	 *
	 * @param MediaObject, a Sage MediaFile
	 */
	public boolean IsDVD(Object MediaObject)
		{
		return (MediaFileAPI.IsDVD(MediaObject) && !MediaFileAPI.IsBluRay(MediaObject));
	}

	public boolean IsBluRay(Object MediaObject) {
		return MediaFileAPI.IsBluRay(MediaObject);
	}

//Common Metadata
	public String GetDescription(Object MediaObject) {
		return ShowAPI.GetShowDescription(MediaObject);
	}
	public String GetMediaEncoding(Object mediafile){
		return MediaFileAPI.GetMediaFileFormatDescription(mediafile);
	}

	public String GetVideoEncoding(Object mediafile){
		String format = GetMediaEncoding(mediafile);
		return format.substring(0, format.indexOf(",")).concat("]");
	}

	public String GetAudioEncoding(Object mediafile){
		String format = GetMediaEncoding(mediafile);
		if (format.indexOf("Subpic") != -1){
			return format.substring(format.indexOf(",") + 2, format.indexOf("Subpic") - 2);
		} else {
			return format.substring(format.indexOf(",") + 2, format.length() - 1);
		}
	}

	public String GetSubpicEncoding(Object mediafile){
		String format = GetMediaEncoding(mediafile);
		if (format.indexOf("Subpic") != -1){
			return format.substring(format.indexOf("Subpic"), format.length() - 1);
		} else {
			return "";
		}
	}

//TV Metadata
	public String GetShowTitle(Object MediaObject) {
		String MediaTitle = new String();
		if (MediaFileAPI.IsTVFile(MediaObject)) {
			MediaTitle = AiringAPI.GetAiringTitle(MediaObject);
		} else if (IsImportedTV(MediaObject)){
			MediaTitle = MediaFileAPI.GetMediaFileMetadata(MediaObject, "MediaTitle");
		} else {
			MediaTitle = ortus.api.GetMediaTitle(MediaObject);
		}

		if (MediaTitle.length() == 0) {
			return "Unknown";
		} else {
			return MediaTitle;
		}
	}

	public String GetMediaTitle(Object mediafile){
		return MediaFileAPI.GetMediaTitle(mediafile);
	}

	/**
	 * Get the episode title for a media object
	 * @param o1 Media object
	 * @return Episode Title
	 */
	public String GetEpisodeTitle(Object MediaObject) {
		if (MediaFileAPI.IsTVFile(MediaObject)) {
			return ShowAPI.GetShowEpisode(MediaObject);
		} else {
			try {
				String s1 = (String) MediaFileAPI.GetMediaFileMetadata(MediaObject, "EpisodeTitle");
				if (s1.isEmpty()) {
					s1 = (String) MediaFileAPI.GetMediaFileMetadata(MediaObject, "MediaTitle");
					if (s1.isEmpty()) {
						s1 = (String) ShowAPI.GetShowEpisode(MediaObject);
						if (s1.isEmpty()) {
							s1 = (String) ShowAPI.GetShowTitle(MediaObject);
						}
					}
				}
				return s1;
			} catch (Exception e) {
				return "";
			}
		}
	}

	/*returns the OriginalAiringDate as a long (in java date format) gathered from the metadta
	 * 0 if it does not exist or catches an error
	 *
	 * @param MediaObject, a sage Airing, Show, or MediaFile
	 */
	public long GetOriginalAirDate(Object MediaObject) {
		if (MediaFileAPI.IsTVFile(MediaObject)) {
			return ShowAPI.GetOriginalAiringDate(MediaObject);
		} else {
			String s1 = MediaFileAPI.GetMediaFileMetadata(MediaObject, "OriginalAirDate");
			//ortus.api.DebugLog(TRACE2, "GetOriginalAirDate: OriginalAirDateString = '" + s1 + "'");

			if (s1.length() == 0) {
				//ortus.api.DebugLog(TRACE2, "GetOriginalAirDate: No Original Air Date");
				return 0;
			}
			try {
				DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				return sdf.parse(s1).getTime() + ortus.api.GetSeasonEpisodeNumber(MediaObject);
			} catch (ParseException e) {
				ortus.api.DebugLog(WARNING, "GetOriginalAirDate: Date Parsing Exception :" + e);
				return 0;
			}
		}
	}

	/*
	 * returns the Disc Number as an integer, 0 if it does not exist
	 *
	 * @param MediaObject, a sage Airing, Show, or MediaFile Object
	 */
	public int GetDiscNumber(Object MediaObject) {
		String s1 = MediaFileAPI.GetMediaFileMetadata(MediaObject, "DiscNumber");

		if (s1.length() == 0) {
			return 0;
		} else {
			return Integer.parseInt(s1);
		}
	}

	/* returns the Season Number as an int, 0 if it does not exist
	 *
	 * @param MediaObject, a sage Airing, Show, or MediaFile Object
	 */
	public int GetSeasonNumber(Object MediaObject) {
		String s1 = MediaFileAPI.GetMediaFileMetadata(MediaObject, "SeasonNumber");

		if (s1.length() == 0) {
			return 0;
		} else {
			return Integer.parseInt(s1);
		}
	}

	/* returns the Episode Number as an int, 0 if it does not exist
	 *
	 * @param MediaObject, a sage Airing, Show, or MediaFile Object
	 */
	public int GetEpisodeNumber(Object MediaObject) {
		String s1 = MediaFileAPI.GetMediaFileMetadata(MediaObject, "EpisodeNumber");

		if (s1.length() == 0) {
			return 0;
		} else {
			return Integer.parseInt(s1);
		}
	}

	//public int GetMediaType(Object mediafile){return 0;}
	//public int GetMediaGroup(Object mediafile){return 0;}
	//public boolean IsTV(Object mediafile){return false;}
	//public boolean IsTVMovie(Object mediafile){return false;}
	//public boolean IsRecorded(Object mediafile){return false;}
	//public boolean IsImported(Object mediafile){return false;}
	//public boolean IsSeries(Object mediafile){return false;}
	//public boolean IsIntelligentRecorded(Object mediafile){return false;}
	public boolean IsFavorite(Object mediafile){return false;}
	public boolean IsHD(Object mediafile){return false;}
	public boolean IsSD(Object mediafile){return false;}
	//public boolean IsMovie(Object mediafile){return false;}
	public boolean IsHDMovie(Object mediafile){return false;}
	public boolean IsSDMovie(Object mediafile){return false;}
	//public boolean IsDVD(Object mediafile){return false;}
	//public boolean IsBluRay(Object mediafile){return false;}

	//Common Metadata
	//public String GetMediaTitle(Object mediafile){return null;}
	public String GetMediaFileID(Object mediafile){return null;}
	//public String GetDescription(Object mediafile){return null;}
	public List<Object> GetGenre(Object mediafile){return null;}
	public String GetImportDate(Object mediafile){return null;}
	public String GetPath(Object mediafile){return null;}
	//public String GetMediaEncoding(Object mediafile){return null;}
	//public String GetVideoEncoding(Object mediafile){return null;}
	//public String GetAudioEncoding(Object mediafile){return null;}
	//public String GetSubpicEncoding(Object mediafile){return null;}

	//TV Series Metadata
	public String GetSeriesTitle(Object mediafile){return null;}
	public String GetSeriesDescription(Object mediafile){return null;}
	public String GetSeriesNetwork(Object mediafile){return null;}
	public long GetSeriesFirstAirDate(Object mediafile){return 0;}
	public long GetSeriesFinalAirDate(Object mediafile){return 0;}
	public boolean IsSeriesStillRunning(Object mediafile){return false;}
	public String GetSeriesStatus(Object mediafile){return null;}
	public String GetSeriesAirDay(Object mediafile){return null;}
	public long GetSeriesRunTime(Object mediafile){return 0;}
	public long GetSeriesNextEpisodeDate(Object mediafile){return 0;}
	public String GetSeriesTVRating(Object mediafile){return null;}
	public int GetSeriesTotalSeasons(Object mediafile){return 0;}
	public int GetSeriesTotalEpisodes(Object mediafile){return 0;}
	public int GetSeriesTotalEpisodesAvailable(Object mediafile){return 0;}

	//TV Metadata
	//public String GetShowTitle(Object mediafile){return null;}
	//public String GetEpisodeTitle(Object mediafile){return null;}
	//public int GetSeasonNumber(Object mediafile){return 0;}
	//public int GetEpisodeNumber(Object mediafile){return 0;}
	//public long GetOriginalAirDate(Object mediafile){return 0;}

	//Movie Metadata
	public String GetMovieTitle(Object mediafile){return null;}
	public String GetReleaseDate(Object mediafile){return null;}
	public String GetMPAARating(Object mediafile){return null;}
	//public int GetDiscNumber(Object mediafile){return 0;}

	//Cast Metadata
	public List<String> GetCast(Object mediafile){return null;}
	public String GetDirector(Object mediafile){return null;}
	public String GetWriter(Object mediafile){return null;}
	public String GetProducer(Object mediafile){return null;}
	public List<String> GetActors(Object mediafile){return null;}

        //Search
        public List<Object> Search(String filter) { return null; }

    @Override
    public void SetDescription(Object mediafile, String title) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void SetMediaType(Object mediafile, Object mediatype) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

	@Override
	public int GetUserRating(Object mediafile) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public int GetSageMediaType(Object smo) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public int GetSageMediaGroup(Object smo) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public String GetSageMediaTitle(Object smo) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public String GetSageDescription(Object smo) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public String GetSageEpisodeTitle(Object smo) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public String GetSageMPAARating(Object smo) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public int GetSageUserRating(Object smo) {
		throw new UnsupportedOperationException("Not supported yet.");
	}
}
